package com.liyunc.demo.resources;
/**
 * Copyright (c) 2018-2022 ThunderSoft
 * All Rights Reserved by Thunder Software Technology Co., Ltd and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with ThunderSoft in writing by applicable law.
 */

import com.liyunc.demo.model.dto.GenericDto;
import com.liyunc.demo.model.dto.ProjectInfoDto;
import com.liyunc.demo.service.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * @author liyuncong
 * @version 1.0
 * @file
 * @brief
 * @details
 * @date 2022-02-18
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2022-02-18               liyuncong        Created
 */
@RestController
@RequestMapping(value = "/api/v1/project", produces = {MediaType.APPLICATION_JSON_VALUE})
public class ProjectResource {

    @Autowired
    private ProjectService projectService;

    @PostMapping(value = "", consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Operation(description = "创建项目")
    public GenericDto<ProjectInfoDto> createProject(@RequestBody ProjectInfoDto infoDto) {
        return GenericDto.of(projectService.createProject(infoDto).orElse(null));
    }

    @PostMapping(value = "update", consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Operation(description = "更新项目")
    public GenericDto<ProjectInfoDto> updateProject(@RequestBody ProjectInfoDto infoDto) {
        return GenericDto.of(projectService.updateProject(infoDto).orElse(null));
    }

    @GetMapping(value = "/{projectId}", params = {"action=delete"})
    @Operation(description = "删除项目")
    public GenericDto<Boolean> deleteSingleProject(
        @Min(value = 1) @PathVariable("projectId") long projectId) {
        projectService.deleteProject(projectId);
        return GenericDto.of(Boolean.TRUE);
    }

    @GetMapping(value = "list")
    @Operation(description = "获取项目列表")
    public GenericDto<List<ProjectInfoDto>> projectList(
        @Valid @Min(0) @RequestParam(value = "startIndex", defaultValue = "0") int startIndex,
        @Valid @Min(1) @RequestParam(value = "pageSize", defaultValue = "10") int pageSize
    ) {
        return GenericDto.of(projectService.getProjectList(startIndex, pageSize));
    }

    @GetMapping(value = "/{projectId}")
    @Operation(description = "根据id获取项目")
    public GenericDto<ProjectInfoDto> getSingleProject(
        @Min(value = 1) @PathVariable("projectId") long projectId) {
        return GenericDto.of(projectService.getSingleProject(projectId).orElse(null));
    }
}
